package com.example.customer01.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.customer01.pojo.Course;
import com.example.customer01.service.CourseService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 课程控制器
 */
@RestController
@RequestMapping("/api/courses")
public class CourseController {

    @Resource
    private CourseService courseService;

    /**
     * 创建新课程
     */
    @PostMapping
    public ResponseEntity<Course> createCourse(@RequestBody Course course) {
        return ResponseEntity.ok(courseService.createCourse(course));
    }

    /**
     * 删除课程
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCourse(@PathVariable Integer id) {
        courseService.deleteCourse(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 更新课程信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<Course> updateCourse(@PathVariable Integer id, @RequestBody Course course) {
        course.setId(id);
        return ResponseEntity.ok(courseService.updateCourse(course));
    }

    /**
     * 获取课程详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<Course> getCourse(@PathVariable Integer id) {
        Course course = courseService.getCourseById(id);
        return course != null ? ResponseEntity.ok(course) : ResponseEntity.notFound().build();
    }

    /**
     * 获取所有课程
     */
    @GetMapping
    public ResponseEntity<List<Course>> getAllCourses() {
        return ResponseEntity.ok(courseService.getAllCourses());
    }

    /**
     * 获取学生的所有课程
     */
    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<Course>> getStudentCourses(@PathVariable Integer studentId) {
        return ResponseEntity.ok(courseService.getCoursesByStudentId(studentId));
    }

    /**
     * 学生选课
     */
    @PostMapping("/{courseId}/enroll/{studentId}")
    public ResponseEntity<Void> enrollStudent(
            @PathVariable Integer courseId,
            @PathVariable Integer studentId,
            @RequestParam String semester) {
        boolean success = courseService.enrollStudent(studentId, courseId, semester);
        return success ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
    }

    /**
     * 学生退课
     */
    @PostMapping("/{courseId}/drop/{studentId}")
    public ResponseEntity<Void> dropCourse(
            @PathVariable Integer courseId,
            @PathVariable Integer studentId) {
        boolean success = courseService.dropCourse(studentId, courseId);
        return success ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
    }

    /**
     * 更新学生课程成绩
     */
    @PutMapping("/{courseId}/grade/{studentId}")
    public ResponseEntity<Void> updateGrade(
            @PathVariable Integer courseId,
            @PathVariable Integer studentId,
            @RequestParam String grade) {
        boolean success = courseService.updateGrade(studentId, courseId, grade);
        return success ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
    }

    /**
     * 分頁查詢課程
     */
    @GetMapping("/page")
    public IPage<Course> getCoursePage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword) {
        Page<Course> page = new Page<>(current, size);
        return courseService.getCoursePage(page, keyword);
    }
} 